package com.dingreading.cloud.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.entity.CouponStaff;
import com.dingreading.cloud.admin.entity.Coupons;
import com.dingreading.cloud.admin.service.CouponStaffLogService;
import com.dingreading.cloud.admin.service.CouponStaffService;
import com.dingreading.cloud.admin.service.CouponsService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/couponStaff")
@Api(tags = "总部-员工的代金券管理")
public class CouponStaffController {

    @Resource
    private CouponsService couponsService;
    @Resource
    private CouponStaffService couponStaffService;
    @Resource
    private CouponStaffLogService couponStaffLogService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "代金券名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "启用状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<CouponStaff> page = this.couponStaffService.pageList(pageUtil, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "根据ID获取信息")
    @PostMapping(value = "/getById", produces = "application/json; charset=UTF-8")
    public R<Object> getById(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        CouponStaff data = couponStaffService.getById(id);
        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        long cnt = couponStaffLogService.getByCouponStaffId(id);
        if (cnt > 0) return R.fail("员工代金券已使用，不能删除");

        long l = couponStaffService.dropData(id);
        if (l <= 0) return R.fail("删除失败");

        return R.ok();
    }


    @ApiOperation(value = "给店长发放代金券")
    @PostMapping(value = "/sendToStaff")
    public R<Object> sendToStaff(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "代金券id") @RequestParam(value = "couponId") Long couponId,
            @ApiParam(value = "发放数量") @RequestParam(value = "num") Integer num,
            @ApiParam(value = "员工集合[{storeUid:'',staffUid:'',fullName:'',storeName:''}]") @RequestParam(value = "staffJson") String staffJson,
            @ApiParam(value = "备注") @RequestParam(value = "remark") String remark
    ) {
        if (num == null || num <= 0) return R.fail("请填写正确的发放数量");

        if (StringUtils.isBlank(staffJson)) return R.fail("请选择员工");

        JSONArray staffList = JSONArray.parseArray(staffJson);
        if (CollectionUtils.isEmpty(staffList)) return R.fail("请选择员工");

        Coupons data = couponsService.getById(couponId);
        if (data == null) return R.fail("代金券获取失败");

        List<CouponStaff> newList = new ArrayList<>();
        for (int i = 0; i < staffList.size(); i++) {
            for (int j = 0; j < num; j++) {
                JSONObject jsonObject = staffList.getJSONObject(i);
                String storeUid = jsonObject.getString("storeUid");
                String staffUid = jsonObject.getString("staffUid");
                if (StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid)) {
                    return R.fail("员工参数错误");
                }
                String fullName = jsonObject.getString("fullName");
                String storeName = jsonObject.getString("storeName");
                CouponStaff couponStaff = new CouponStaff();
                couponStaff.setStoreUid(storeUid);
                couponStaff.setStaffUid(staffUid);
                couponStaff.setFullName(fullName);
                couponStaff.setStoreName(storeName);
                couponStaff.setCouponId(data.getId());
                couponStaff.setTypeId(data.getTypeId());
                couponStaff.setTypeName(data.getTypeName());
                couponStaff.setConformMoney(data.getConformMoney());
                couponStaff.setCouponName(data.getCouponName());
                couponStaff.setCouponMoney(data.getCouponMoney());
                couponStaff.setUsableMoney(data.getCouponMoney());
                couponStaff.setGoodsIds(data.getGoodsIds());
                couponStaff.setGoodsCategoryIds(data.getGoodsCategoryIds());
                couponStaff.setUsedMoney(0d);
                couponStaff.setEnabled(1);
                couponStaff.setRemark(remark);
                couponStaff.setAddStaffUid(headStaffUid);
                couponStaff.setAddStaffName(headStaffName);
                newList.add(couponStaff);
            }
        }

        couponStaffService.saveOrUpdateBatch(newList);
        return R.ok();
    }


    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        couponStaffService.updateEnabled(id, enabled);
        return R.ok();
    }


    @ApiOperation(value = "获取代金券下的员工")
    @PostMapping(value = "/getCouponStaff", produces = "application/json; charset=UTF-8")
    public R<Object> getCouponStaff(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "代金券id") @RequestParam(value = "couponId") Long couponId,
            @ApiParam(value = "门店名/员工名") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        List<CouponStaff> couponStaffList = couponStaffService.getByCouponId(couponId, 1, keyword, null);
        return R.ok(couponStaffList);
    }
}